package com.hyq.learn.ai.rest;

import com.hyq.learn.ai.service.ChatService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
 * @author heyuqin
 * @date 2025/10/16 16:13
 */
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ChatController {

    private final ChatService chatService;

    public ChatController(ChatService chatService) {
        this.chatService = chatService;
    }

    @GetMapping(value = "/chat_stream", produces = MediaType.TEXT_PLAIN_VALUE)
    public StreamingResponseBody chatStream(@RequestParam String prompt) {
        return outputStream -> chatService.streamChat(prompt, outputStream);
    }
}
